//
//  addAddressManager.m
//  FlowerRainGround
//
//  Created by lanou on 16/4/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "addAddressManager.h"

@implementation addAddressManager


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.dataBase = [DBManger shareInstanceDbName:SQLITENAME].dateBase;
    }
    return self;
}

//创建表
- (void) creatTable
{
    //查询表
    FMResultSet *set = [self.dataBase executeQuery:[NSString stringWithFormat:@"select count (*) from sqlite_master where type = 'table' and name = '%@' ", AddressTable]];
    [set next];
    NSInteger result = [set intForColumnIndex:0];
    if (result) {
        NSLog(@"数据库表已存在");
    }else{
        NSString *string = [NSString stringWithFormat:@"CREATE TABLE %@(id INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL,recipite text, phoneNumber text,ZipCoding text, nowAddress text, infoAddress text )", AddressTable];
        BOOL result = [self.dataBase executeUpdate:string];
        if (result) {
            NSLog(@"表创建成功");
        }else{
            NSString *string = [self.dataBase lastErrorMessage];
            NSLog(@"创建失败%@", string);
        }
    }
    
}


//增加地址信息
- (void) addAddressModel:(addressModel *)addressmodel
{
    NSString *string = [NSString stringWithFormat:@"INSERT INTO %@ (recipite, phoneNumber, ZipCoding , nowAddress, infoAddress ) values (?, ?, ?, ? ,?)", AddressTable];
    
    NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:0];
    if (addressmodel.recipite) {
        [array1 addObject:addressmodel.recipite];
    }
    if (addressmodel.phoneNumber) {
        [array1 addObject:addressmodel.phoneNumber];
    }
    if (addressmodel.ZipCoding) {
        [array1 addObject:addressmodel.ZipCoding];
    }
    if (addressmodel.nowAddress) {
        [array1 addObject:addressmodel.nowAddress];
    }
    if (addressmodel.infoAddress) {
        [array1 addObject:addressmodel.infoAddress];
    }
    
    
    BOOL result = [self.dataBase executeUpdate:string withArgumentsInArray:array1];
    if (result) {
        NSLog(@"添加成功");
    }
    else{
        NSString *error = [self.dataBase lastErrorMessage];
        NSLog(@"添加失败%@", error);
    }
}

//删除
- (void) cancelAddressModel:(addressModel *)addressmodel;{
    
    NSString *string = [NSString stringWithFormat:@"delete from %@ where recipite = ?", AddressTable];
    BOOL result = [self.dataBase executeUpdate:string, addressmodel.recipite];
    if (result) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败：%@",[self.dataBase lastErrorMessage] );
    }
    
}

//查询所有的信息
- (NSArray *) selectAll {
    NSString *string = [NSString stringWithFormat:@"select  * from %@", AddressTable];
    FMResultSet *set = [self.dataBase executeQuery:string];
    NSMutableArray *array = [NSMutableArray array ];
    while ([set next]) {
        addressModel *model = [[addressModel alloc] init];
        model.recipite = [set stringForColumn:@"recipite"];
        model.phoneNumber = [set stringForColumn:@"phoneNumber"];
        model.ZipCoding = [set stringForColumn:@"ZipCoding"];
        model.nowAddress = [set stringForColumn:@"nowAddress"];
        model.infoAddress = [set stringForColumn:@"infoAddress"];
        [array addObject:model];
    }
    [set close];
    return array;
}
@end
